﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebMvc_Survey.ApiControllers;
using WebMvc_Survey.Attributes;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace WebMvc_Survey.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 分享页
        /// </summary>
        /// <param name="shareId"></param>
        /// <returns></returns>
        public IActionResult Index(string shareId,string b)
        {
            ViewBag.B = b;
            if (!string.IsNullOrEmpty(shareId))
                Response.Cookies.Append("ShareId", shareId);
            Response.Redirect("/Login");
            return View();
        }

        /// <summary>
        /// 排名
        /// </summary>
        /// <returns></returns>
        public IActionResult Share(string shareId)
        {
            if (Request.Cookies["UserId"] == null)
            {
                return Redirect("/Home?shareId=" + shareId);
            }
            else
            {
                Survey_ShareController _Survey_ShareController = new Survey_ShareController();
                string userid = System.Convert.ToString(Request.Cookies["UserId"]);
                ViewBag.Share = _Survey_ShareController.Get(userid);
                ViewBag.Num = _Survey_ShareController.GetNum(userid);
                NewBee.Core.Data.Model.PageParam _PageParam = new NewBee.Core.Data.Model.PageParam();
                _PageParam.rows = 100;
                ViewBag.PageList = _Survey_ShareController.GetPageList(_PageParam);

                if (string.IsNullOrEmpty(shareId))
                    Response.Redirect("/Home/Share?shareId=" + ((NewBee.Core.Models.Survey_Share)ViewBag.Share).ID);
            }
            //Response.Cookies.Append("Test", "Success!");
            //ViewBag.Test = Request.Cookies["Test"];
            return View();
        }

        /// <summary>
        /// 排名
        /// </summary>
        /// <returns></returns>
        public IActionResult Ranking()
        {
            //Survey_ShareController _Survey_ShareController = new Survey_ShareController();
            //string userid = System.Convert.ToString(Request.Cookies["UserId"]);
            //ViewBag.Share = _Survey_ShareController.Get(userid);
            //ViewBag.Num = _Survey_ShareController.GetNum(userid);
            //NewBee.Core.Data.Model.PageParam _PageParam = new NewBee.Core.Data.Model.PageParam();
            //_PageParam.rows = 100;
            //ViewBag.PageList = _Survey_ShareController.GetPageList(_PageParam);
            //Response.Cookies.Append("Test", "Success!");
            //ViewBag.Test = Request.Cookies["Test"];
            return View();
        }

        /// <summary>
        /// 问卷
        /// </summary>
        /// <returns></returns>
        [Login]
        public IActionResult FormResult()
        {
            Survey_ResultController _Survey_ResultController = new Survey_ResultController();
            string userid = System.Convert.ToString(Request.Cookies["UserId"]);
            string username = System.Convert.ToString(Request.Cookies["UserName"]);
            string remark = System.Convert.ToString(Request.Cookies["MobilePhone"]);
            string formid = "5d97b5a7-3bbf-4d4d-9f8f-599810bbcaeb";
            string shareid = System.Convert.ToString(Request.Cookies["ShareId"]);
            NewBee.Core.Models.Survey_Result _Survey_Result = _Survey_ResultController.Get(userid, formid);
            if (_Survey_Result == null)
            {
                _Survey_Result = new NewBee.Core.Models.Survey_Result();
                _Survey_Result.UserID = userid;
                if (string.IsNullOrEmpty(username))
                    username = userid;
                _Survey_Result.UserName = username;
                if(string.IsNullOrEmpty(shareid))
                    _Survey_Result.ShareID = Guid.Parse("07cc3369-e0b4-4a5a-89ea-37caf9b29676");
                else
                    _Survey_Result.ShareID = Guid.Parse(shareid);
                _Survey_Result.FormID = Guid.Parse(formid);
                _Survey_Result.Remark = remark;
                _Survey_ResultController.Post(_Survey_Result);
                _Survey_Result = _Survey_ResultController.Get(userid, formid);
            }
            if (string.IsNullOrEmpty(_Survey_Result.Data)) {
                NewBee.Core.Models.Survey_ResultData20160711 _Survey_ResultData20160711 = new NewBee.Core.Models.Survey_ResultData20160711();
                _Survey_ResultData20160711.ID = _Survey_Result.ID;
                _Survey_Result.Data = _Survey_ResultData20160711.ToJson();
            }
            else {
                if (_Survey_Result.Status == 1)
                    Response.Redirect("/Home/FormDone"); 
                    //Response.Redirect("/Home/Prize");
                if (_Survey_Result.Status == 2)
                    Response.Redirect("/Home/FormDone");
                //Response.Redirect("/Home/PrizeResult");
            }
                
            ViewBag.Result = _Survey_Result;
            return View();
        }

        [Login]
        public IActionResult FormDone()
        {
            return View();
        }

        /// <summary>
        /// 摇奖
        /// </summary>
        /// <returns></returns>
        [Login]
        public IActionResult Prize()
        {
            Response.Redirect("/Home/FormDone");

            Survey_ResultController _Survey_ResultController = new Survey_ResultController();
            string userid = System.Convert.ToString(Request.Cookies["UserId"]);
            string formid = "5d97b5a7-3bbf-4d4d-9f8f-599810bbcaeb";
            NewBee.Core.Models.Survey_Result _Survey_Result = _Survey_ResultController.Get(userid, formid);
            if(_Survey_Result==null)
                Response.Redirect("/Home/FormResult");

            if (_Survey_Result.Status == 0)
                Response.Redirect("/Home/FormResult");

            if (_Survey_Result.Status == 2)
                Response.Redirect("/Home/PrizeResult");


            ViewBag.ID = _Survey_Result.ID;
            return View();
        }

        /// <summary>
        /// 摇奖结果
        /// </summary>
        /// <returns></returns>
        [Login]
        public IActionResult PrizeResult()
        {
            Response.Redirect("/Home/FormDone");
            Survey_ResultController _Survey_ResultController = new Survey_ResultController();
            string userid = System.Convert.ToString(Request.Cookies["UserId"]);
            string formid = "5d97b5a7-3bbf-4d4d-9f8f-599810bbcaeb";
            ViewBag.Result = _Survey_ResultController.Get(userid, formid);
            return View();
        }

    }
}
